package org.game.msgBuilder;

import java.util.List;
import java.util.Map;

import org.cache.DictionariesCache;
import org.constant.MailConstant;
import org.game.db.bean.Mail;
import org.game.db.bean.Player;
import org.game.db.bean.ServerMail;
import org.game.db.vo.BaseMail;
import org.game.protobuf.s2c.S2CMailMsg.MailInfo;
import org.game.protobuf.s2c.S2CMailMsg.MailInfoList;
import org.game.protobuf.s2c.S2CShareMsg.ChangeType;
import org.game.protobuf.s2c.S2CShareMsg.RewardsMsg;

import com.google.common.collect.Lists;
import com.sojoys.artifact.plugin.i18n.I18NPlugin;
import com.sojoys.artifact.tools.ToolDate;
import com.sojoys.artifact.tools.ToolMap;
import com.xiaoleilu.hutool.util.StrUtil;

/**
 * @author : DengYing
 * @CreateDate : 2017年8月15日 下午4:21:17
 * @Description ：Please describe this document
 */
public class MailMsgBuilder {
	
	
	public static MailInfo createMailInfo(Player player,ChangeType changeType,BaseMail baseMail,int assignStatus){
		MailInfo.Builder builder = MailInfo.newBuilder();
		builder.setCt(changeType);
		
		if (baseMail instanceof ServerMail) {
			builder.setId(Long.toString(((ServerMail)baseMail).getId()));
		}else {
			builder.setId(Long.toString(((Mail)baseMail).getId()));
		}
		
		builder.setSenderId(baseMail.getSenderId());
		builder.setSenderName("");
		
		builder.setCreateTime((int)(baseMail.getCreateDate().getTime() / ToolDate.TIME_SECOND));
		builder.setModifiedTime((int)(baseMail.getModifiedDate().getTime() / ToolDate.TIME_SECOND));
		
		// 修改&删除 不发送
		if (assignStatus == -1 || assignStatus == MailConstant.STATUS_DEFAULT) {
			String title = "";
			String body = "";
			switch (baseMail.getTextType()) {
			case MailConstant.TEXT_TYPE_CONFIG:
				title = I18NPlugin.get(player.getLanguage(), baseMail.getTitle());
				body = I18NPlugin.get(player.getLanguage(), baseMail.getBody());
				break;
			case MailConstant.TEXT_TYPE_DICT:
				title = DictionariesCache.getContent(player.getLanguage(), baseMail.getTitle());
				body = DictionariesCache.getContent(player.getLanguage(), baseMail.getBody());
				break;
			}
			
			// 参数格式化
			if (baseMail.getBodyParam() != null && !baseMail.getBodyParam().isEmpty()) {
				body = StrUtil.format(body, baseMail.getBodyParam());
			}
			
			builder.setTitle(title);
			builder.setBody(body);
			// 附件
			Map<Integer, Integer> accessory = baseMail.getItems();
			for (Map.Entry<Integer, Integer> e : accessory.entrySet()) {
				RewardsMsg.Builder b = RewardsMsg.newBuilder();
				b.setId(e.getKey());
				b.setNum(e.getValue());
				builder.addRewards(b.build());
			}
		}
		
		// 邮件状态
		if (assignStatus != -1) {
			builder.setStatus(assignStatus);	
		}else {
			if (baseMail instanceof ServerMail) {
				Map<Integer,Integer> status = ((ServerMail) baseMail).getPidsStatus();
				int statusVal = ToolMap.getInt(player.getId(), status , MailConstant.STATUS_DEFAULT);
				builder.setStatus(statusVal);
			}else if(baseMail instanceof Mail){
				builder.setStatus(((Mail)baseMail).getStatus());			
			} 
		}
		
		return builder.build();
	}

	
	public static MailInfo createMailInfo(Player player, ChangeType changeType, BaseMail baseMail) {
		return createMailInfo(player, changeType, baseMail, -1);
	}
	
	
	public static MailInfoList createMailInfoList(Player player,ChangeType type,List<BaseMail> mails){
		MailInfoList.Builder builder = MailInfoList.newBuilder();
		for (BaseMail mail : mails) {
			builder.addMails(createMailInfo(player,type,mail));
		}
		return builder.build();
	}
	
	public static MailInfoList createMailInfoList(Player player,ChangeType type,List<BaseMail> mails,int status){
		MailInfoList.Builder builder = MailInfoList.newBuilder();
		for (BaseMail mail : mails) {
			builder.addMails(createMailInfo(player,type,mail,status));
		}
		return builder.build();
	}
	
	public static List<MailInfo> createMailInfoArray(Player player,ChangeType type,List<BaseMail> mails){
		List<MailInfo> list = Lists.newArrayList();
		for (BaseMail mail : mails) {
			list.add(createMailInfo(player,type,mail));
		}
		return list;
	}
}
